<template lang="pug">
.list
  .header
    .header-search
      el-input(placeholder="材料名称" prefix-icon="el-icon-search" v-model="sampleName" @change="search")
      el-select(placeholder="选择供应商" filterable prefix-icon="el-icon-search" clearable v-model="supplierId" @change="search")
        el-option(v-for="item in supplierList", :key="item.objectId", :label="item.companyName", :value="item.objectId")
      el-select(placeholder="请选择品牌名称" filterable prefix-icon="el-icon-search" clearable v-model="brandId" @change="search")
        el-option(v-for="item in brandList", :key="item.id", :label="item.name", :value="item.id")
      el-cascader(placeholder="请选择样品分类" :props="cascaderProps" v-model="classificationId" clearable @change="search")
  .list-table
    el-table(
      height="100%"
      :data="list"
      :header-cell-style="{ fontSize: '14px', color: '#64698F', background: '#F8F7FC' }"
      border
      style="width: 100%"
      v-loading="loading"
      empty-text="未找到符合条件的内容"
    )
      el-table-column(align="center" min-width="80" label="材料图片")
        template(slot-scope="scope")
          .table-item-bd.flex
            .sample-photo
              img(:src="scope.row.picUrl")
      el-table-column(align="center" min-width="150" label="材料信息")
        template(slot-scope="scope")
          .table-item-bd.left
            .table-item-value {{scope.row.name}}
            .table-item-value 品牌：{{scope.row.brandName}}
            .table-item-value 供应商：{{scope.row.companyContactName}}
      el-table-column(align="center" min-width="120" label="价格/分类")
        template(slot-scope="scope")
          .table-item-bd.left
            .table-item-value 价格：{{scope.row.priceRange}}
            .table-item-value 分类：{{scope.row.categoryName}}
      el-table-column(align="center" label="材料规格")
        template(slot-scope="scope")
          .table-item-bd.left
            .table-item-value(v-for="item in scope.row.attributes") {{item.name}}：{{item.values}}
      el-table-column(align="center" label="操作")
        template(slot-scope="scope")
          .table-item-bd.flex-col
            el-button(size="mini" type="info" plain @click="edit(scope.row)") 编辑
            el-button(size="mini" type="warning" plain @click="addSample(scope.row)") 入库
  .table-pagination
    el-pagination(
      background
      layout="prev, pager, next, jumper"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
      @current-change="currentChange"
    )
  add-sample-spec(:show="showSpecModal" :item="currentItem" @close="closeSpecModal" @confirm="confirmSpecModal")
</template>

<script>
import { getRoomMaterialList, getCategoryList, getBrandList, putOnOrOff, deleteMaterial, getSuppliersList, updateSampleLabel, postDeleteSample } from 'api'
import sampleRecordModal from '../components/sampleRecordModal'
import changeStockModal from '../components/changeStockModal'
import addSampleSpec from '../components/addSampleSpec'
export default {
  name: 'storageMaterial',
  data () {
    return {
      page: 1,
      pageSize: 20,
      total: 0,
      sampleName: '',
      brandId: '',
      supplierId: '',
      status: '',
      classificationId: '',
      isOnSales: '',
      list: [],
      loading: false,
      cascaderProps: {
        lazy: true,
        value: 'id',
        label: 'title',
        children: 'children',
        emitPath: false,
        lazyLoad: ((node, resolve) => {
          if (!node.level) this.getCategoryList(node, resolve)
          else resolve()
        })
      },
      currentList: [],
      cateList: [],
      supplierList: [],
      brandList: [],
      currentItem: {},
      showRecordDialog: false,
      showStockDialog: false,
      showSpecModal: false
    }
  },
  components: {
    sampleRecordModal,
    changeStockModal,
    addSampleSpec
  },
  methods: {
    addSample (row) {
      this.currentItem = row
      this.showSpecModal = true
    },
    closeSpecModal () {
      this.currentItem = {}
      this.showSpecModal = false
    },
    confirmSpecModal () {
      this.currentItem = {}
      this.showSpecModal = false
      this.getList()
    },
    search () {
      this.page = 1
      this.getList()
    },
    edit (row) {
      this.$router.push({ name: 'storageUpload', params: { id: row.id }, query: { form: 'material' } })
    },
    changeStock (row) {
      this.currentItem = row
      this.showStockDialog = true
    },
    closeStockDialog () {
      this.currentItem = {}
      this.showStockDialog = false
    },
    saveCount () {
      this.currentItem = {}
      this.showStockDialog = false
      this.getList()
    },
    currentChange (page) {
      this.page = page
      this.getList()
      // this.$router.replace({ name: this.$route.name, query: { ...this.$route.query, page: this.page } })
    },
    async getList () {
      try {
        this.loading = true
        let query = { ...this.$route.query }
        query.currentPage = this.page
        query.pageLines = this.pageSize
        if (this.sampleName) query.name = this.sampleName
        if (this.brandId) query.brandId = this.brandId
        if (this.supplierId) query.CompanyContactId = this.supplierId
        if (this.classificationId) query.categoryId = this.classificationId
        const { ResultSet } = await this.$axios.post(getRoomMaterialList, { ...query })
        this.total = ResultSet.recordNumber
        this.list = ResultSet.userData.materialsList
        this.loading = false
      } catch (err) {
        this.loading = false
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async getCategoryList (node, resolve) {
      try {
        const { ResultSet } = await this.$axios.post(getCategoryList)
        resolve(ResultSet.userData.result)
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async getBrandList () {
      try {
        const { ResultSet } = await this.$axios.post(getBrandList)
        this.brandList = ResultSet.userData.loadBrandList
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async getSuppliersList () {
      try {
        const { ResultSet } = await this.$axios.post(getSuppliersList, { supplierStatus: true, reviewStatus: 2, currentPage: 1, pageLines: 999999 })
        let hash = {}
        ResultSet.userData.result = ResultSet.userData.result.reduce(function(item, next) {
            hash[next.objectId] ? '' : hash[next.objectId] = true && item.push(next);
            return item
        }, [])
        this.supplierList = ResultSet.userData.result
        // this.supplierList = ResultSet.userData.result
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async init () {
      await this.getSuppliersList()
      this.getList()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      // vm.getCategoryList()
      vm.getBrandList()
      vm.init()
    })
  },
  beforeRouteUpdate (to, from, next) {
    next()
    this.init()
  }
} 
</script>

<style lang="scss" scoped>
.list {
  width: 100%;
  height: 100%;
  background: #FFFFFF;
  border: 1px solid #F3F3F4;
  box-sizing: border-box;
  box-shadow: 0px 4px 0px #F3F3F4;
  border-radius: 10px;
  padding: 0 40px;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
  .el-input {
    width: 202px;
    margin-right: 16px;
  }
  .el-select {
    margin-right: 16px;
  }
  .el-cascader {
    margin-right: 16px;
  }
  .header-search {
    display: flex;
    align-items: center;
  }
  .header-info {
    font-size: 14px;
    line-height: 22px;
    color: #8B8EAB;
  }
}
.list-table {
  height: calc(100% - 150px);
}
.table-pagination {
  padding: 24px 0;
  text-align: right;
}
.flex {
  display: flex;
  align-items: center;
  justify-content: center;
}
.table-item-bd {
  width: 100%;
  &.left {
    text-align: left;
  }
  &.click {
    cursor: pointer;
  }
  .table-item-head {
    width: 24px;
    height: 24px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    border-radius: 4px;
    margin-right: 8px;
  }
  .table-item-name {
    font-size: 14px;
    line-height: 22px;
    color: #FF2E42;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .table-item-sales {
    display: flex;
    align-items: center;
  }
  .item-sales {
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 5px 8px;
    border: 1px solid #F7DCB7;
    box-sizing: border-box;
    border-radius: 2px;
    &.success {
      border-color: #C0E5D1;
      background: #F2FAF6;
      color: #32C37D;
    }
    &.warning {
      border-color: #F7DCB7;
      background: #FFF7EC;
      color: #FC9700;
    }
    &.info {
      border-color: #B2B4C7;
      background: #F6F6F9;
      color: #8B8EAB;
    }
    span {
      display: inline-block;
      font-size: 14px;
      line-height: 22px;
    }
    .iconfont {
      margin-left: 6px;
      font-size: 16px;
      color: #CCD2E3;
    }
  }
}
.table-item-status {
  display: flex;
  align-items: center;
  .status-point {
    position: relative;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 4px;
    &:before {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      width: 4px;
      height: 4px;
      transform: translate(-50%, -50%);
    }
    &.success {
      background: rgba(48, 217, 135, .5);
      &:before {
        background: #30D987;
      }
    }
    &.warning {
      background: rgba(251, 177, 66, .5);
      &:before {
        background: #FBB142;
      }
    }
    &.normal {
      background: rgba(134, 118, 255, .5);
      &:before {
        background: #8676FF;
      }
    }
  }
  .iconfont {
    font-size: 16px;
    margin-right: 8px;
  }
  .icon-normal {
    color: #32C37D;
  }
  .icon-block-up {
    color: #FBB142;
  }
}
.table-item-handle {
  display: flex;
  .table-item-btn {
    margin-right: 8px;
    padding: 5px 8px;
    font-size: 12px;
    line-height: 20px;
    border-radius: 6px;
    cursor: pointer;
    &.warning {
      background: #FDEFF1;
      color: #FF2E42;
    }
    &.normal {
      background: #F3F0FF;
      color: #7362F0;
    }
    &.info {
      background: #F6F6F9;
      color: #3E4373;
    }
  }
}
.sample-photo {
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    max-width: 100%;
    max-height: 100%;
  }
}
.table-item-switch {
  margin: 5px 0;
}
.flex-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .el-button {
    margin-left: 0;
    margin-bottom: 5px;
  }
}
.table-item-change {
  text-align: center;
  color: #FF2E42;
  cursor: pointer;
}
</style>
